<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="Hello Pro"></u-loading-page>
		<view v-if="!loadding" class="main">
			<view class="description">
				认购描述：
			</view>
			<view class="list">
				<view class="items">
					<view class="title">
						单价
					</view>
					<view class="vals">
						{{price}}
					</view>
				</view>
				<u-line></u-line>
				<view class="items">
					<view class="title">
						数量
					</view>
					<view class="vals">
						<u--input :placeholder="superSubcMsg" border="bottom" inputAlign="right" type="number"
							fontSize="15px" v-model="nums"></u--input>
					</view>
				</view>
			</view>
			<view class="pays">
				<view class="title">
					支付方式
				</view>
				<u-line></u-line>
				<radio-group @change="payTypeCheck">
					<view class="item">
						<view class="tit">
							<view class="lefts">
								<u-icon name="weixin-circle-fill" color="#19be6b" size="28"></u-icon>
							</view>
							<text>微信</text>
						</view>
						<view class="selects">
							<radio value="1" :checked="pay_type == 1" />
						</view>
					</view>
					<u-line dashed></u-line>
					<view class="item">
						<view class="tit">
							<view class="lefts">
								<u-icon name="red-packet-fill" color="#fa3534" size="28"></u-icon>
							</view>
							<text>余额</text>
							<text v-if="balace > 0" class="balace">({{balace}})</text>
						</view>
						<view class="selects">
							<radio value="2" :checked="pay_type == 2" />
						</view>
					</view>
				</radio-group>
			</view>
			<view class="buy" @click="buy">
				购   买
			</view>
		</view>
	</view>
</template>

<script>
	/*#ifdef H5*/
	var wx = require('@/util/jweixin-module.js');
	/*#endif*/
	export default {
		data() {
			return {
				loadding: true,
				price: '69',
				pay_type: 1,
				balace: 0,
				superSubcNum: 0,
				superSubcMsg: '',
				nums: '',
				userinfo: {}
			}
		},
		onLoad() {
			var that = this
			this.userinfo = uni.getStorageSync('userinfo');
			this.getBalace()
			this.singlePrice()
			this.superSubcNumFun()
		},
		methods: {
			// 获取余额
			async getBalace() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'balance/getTotal',
					header: {
						'token': this.userinfo.token
					}
				});
				this.loadding = false
				if (res.code == 1) {
					this.balace = res.data
				}
			},
			// 认购单价
			async singlePrice() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Goods/single_price',
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					this.price = res.data
				}
			},
			// 获取上级认购数
			async superSubcNumFun() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Buying/super_subc_nums',
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					this.superSubcNum = res.data.nums
					this.superSubcMsg = '最大认购数：' + res.data.nums
				}
			},
			// 支付方式切换
			payTypeCheck(e) {
				this.pay_type = e.detail.value
			},
			// 购买
			buy() {
				var that = this
				if (this.nums <= 0) {
					uni.showToast({
						title: '请输入认购量',
						icon: "none"
					});
					return false
				} else {
					if (this.nums > this.superSubcNum) {
						uni.showToast({
							title: '认购数量超出限制',
							icon: "none"
						});
						return false
					}
				}
				// if (this.pay_type == 1) {
				// 	uni.showToast({
				// 		title: '微信支付正在开发中...',
				// 		icon: "none"
				// 	});
				// 	return false
				// }
				if (this.pay_type == 2 && this.nums*this.price*100 > this.balace*100) {
					uni.showToast({
						title: '余额不足',
						icon: "none"
					});
					return false
				}
				uni.showModal({
					title: '提示',
					content: '确定要认购吗？',
					success: function(res) {
						if (res.confirm) {
							that.submits()
						}
					}
				});
			},
			// 购买提交
			async submits() {
				var payTyp = 'YE'
				const openid = uni.getStorageSync('openid')
				if (this.pay_type == 1) {
					payTyp = 'W'
				}
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Goods/sub_buy',
					data: {
						price: this.price,
						nums: this.nums,
						payTyp: payTyp,
						openid: openid
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.showToast({
					title: res.msg,
					icon: "none"
				});
				if (res.code == 1) {
					if (this.pay_type == 1) {
						this.wxpay(res.data)
					} else if (this.pay_type == 2) {
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1500);
					}
				}
			},
			// 微信支付
			async wxpay(payInfo) {
				var that = this
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: payInfo.appId, // 必填，公众号的唯一标识
					timestamp: payInfo.timeStamp, // 必填，生成签名的时间戳
					nonceStr: payInfo.nonceStr, // 必填，生成签名的随机串
					signature: payInfo.paySign, // 必填，签名
					jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表
				})
				wx.ready(function() {
					wx.chooseWXPay({
						timestamp: payInfo.timeStamp, // 时间戳
						nonceStr: payInfo.nonceStr, // 随机数
						package: payInfo.package, //
						signType: payInfo.signType,
						paySign: payInfo.paySign, // 签名
						success: function() {
							uni.showToast({
								title: '支付成功'
							})
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1500)
						},
						cancel: function() {
							uni.showToast({
								title: '取消支付',
								icon: 'none'
							})
						},
						fail: function() {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}

	.main {
		padding: 20rpx;
		font-size: 28rpx;

		.description {
			background-color: #ffffff;
			padding: 20rpx;
			border-radius: 16rpx;
		}

		.list {
			background-color: #ffffff;
			margin-top: 20rpx;
			padding: 0 20rpx;

			.items {
				padding: 20rpx 0;
				display: flex;
				justify-content: space-between;
				border-radius: 16rpx;
				line-height: 70rpx;
				font-weight: bold;

				.title {
					font-size: 29rpx;
				}

				.vals {
					color: #fa3534;
				}
			}
		}

		.pays {
			padding: 0 20rpx;
			margin-top: 20rpx;
			background-color: #ffffff;
			.title {
				padding: 20rpx 0;
			}
			.item {
				padding: 20rpx 0;
				display: flex;
				justify-content: space-between;
				.tit {
					.lefts {
						float: left;
					}
					text {
						margin-top: 8rpx;
						float: left;
						margin-left: 16rpx;
					}
					.balace {
						margin-top: 12rpx;
						font-size: 24rpx;
						color: #c8c9cc;
					}
				}
			}
		}
		.buy {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			text-align: center;
			background-color: #19be6b;
			color: #ffffff;
			font-size: 38rpx;
			padding: 40rpx;
		}
	}
</style>